import {createRouter, createWebHistory, RouteRecordRaw} from 'vue-router'
import HelloWorld from '../components/HelloWorld.vue'

// 加载条
// @ts-ignore
import NProgress from "nprogress";
import "nprogress/nprogress.css";

const routes: Array<RouteRecordRaw> = [
    {
        path: '',
        component: () => import( '../views/Index/Index.vue'),
        children: [
            {
                path: '/table',
                name: 'table',
                component: () => import( '../views/List/Table.vue'),
                meta: {
                    title: "table",
                },
            }, {
                path: '/chart',
                name: 'chart',
                component: () => import( '../views/List/Chart.vue'),
                meta: {
                    title: "chart",
                },
            }, {
                path: '/webscoket',
                name: 'webscoket',
                component: () => import( '../views/List/WebSocket.vue'),
                meta: {
                    title: "webscoket",
                },
            }, {
                path: '/upload',
                name: 'upload',
                component: () => import( '../views/List/Upload.vue'),
                meta: {
                    title: "upload",
                },
            }, {
                path: '/form',
                name: 'form',
                component: () => import( '../views/List/Form.vue'),
                meta: {
                    title: "form",
                },
            }, {
                path: '/pAndC',
                name: 'pAndC',
                component: () => import( '../views/List/pAndC.vue'),
                meta: {
                    title: "pAndC",
                },
            }, {
                path: '/timePage',
                name: 'timePage',
                component: () => import( '../views/EVA/timePage.vue'),
                meta: {
                    title: "timePage",
                },
            }, {
                path: '/handPage',
                name: 'handPage',
                component: () => import( '../views/EVA/handPage.vue'),
                meta: {
                    title: "handPage",
                },
            },
        ]
    },
    {
        path: '/login',
        name: 'login',
        component: () => import( '../views/Auth/Login.vue'),
    },
    {
        path: '/404',
        name: '404',
        component: () => import( '../views/Error/404.vue'),
    },
    {
        path: '/:currentPath(.*)*', // 路由未匹配到，进入这个
        redirect: (_) => {
            return {path: '/404'}
        },
    },
]
const router = createRouter({
    history: createWebHistory(''),
    routes,
    scrollBehavior(to, from, savedPosition) {
        return {
            el: '#app',
            top: 0,
            behavior: 'smooth',
        }
    },
})


// 路由守卫
router.beforeEach((to, from, next) => {
    NProgress.start();
    // const isLogin = !!localStorage.token;
    const isLogin = "test"
    if (to.path === "/login" ||
        to.path === "/register") {
        //'login'和'register'相当于是路由白名单
        next();
    } else {
        //如果token存在，就正常跳转，如果不存在，则说明未登陆，则跳转到'login'
        isLogin ? next() : next("/login");
        next();
    }
});

router.afterEach(() => {
    NProgress.done();
});


export default router